<?php 
if (!defined('BASEPATH')){
	exit('No direct script access allowed');
}

/**
 * Ketoan Controller
 * Build by Phuc Nguyen
 * Contact : nguyenvanphuc0626@gmail.com
 */

class Ketoan extends BACKEND_Controller {

	public function __construct() {
		parent::__construct();
		$this->load->language('ketoan');
		$this->load->language('button');
		if($this->database_connect_status){
			$this->load->model('ketoan_model');
			$this->set_controller('ketoan');
			$this->set_model($this->ketoan_model);
                        $this->load->model('paymentDebts_model');
		}
                $this->load->library('bookinglib');
                $this->bookinglib = new bookinglib();
	}
        
        private function menuTop(){
            $this->load->model('stats_model');
            $user_info                          = $this->stats_model->get_user_info($this->stats_model->get_user_id());
            $this->view_data['role_by_group']   = $user_info['role_list'];
            $this->view_data['username']        = $user_info['username'];
            return $this->view_data;
        }
        
        public function index() {		
		$this->view_data	= array();
		$this->view_data['flash_message_error']         = $this->session->flashdata('flash_message_error');
		$this->view_data['flash_message']		= $this->session->flashdata('flash_message');
		$this->export();
		if($this->database_connect_status){
                        $this->menuTop();
                        $this->load->view('auth/ketoan/index', $this->view_data);
                } else {
                        $this->view_data['flash_message']	= $this->lang->line('database_connect_failed');
                        $this->load->view('templates/backend/error', $this->view_data);	
                }
	}
        
        public function export(){
            //export data to excel
               if(isset($_POST['submit']) && $_POST['submit'] == 'export'){
                    $nowday                      = ($this->input->post('datepicker1'))?strtotime($this->input->post('datepicker1')):'';
                    $endday                      = ($this->input->post('datepicker1'))?strtotime($this->input->post('datepicker1').' 24:00:00'):'';

                    if($nowday == ""){
                        die(" Vui lòng điền ít nhất 1 thông tin!");
                    }

                    $result         =   $this->ketoan_model->export($nowday,$endday);
                    
                    $lists          =   array();
                    foreach($result as $key => $values){
                        
                        $ship = json_decode($values->booking);
                        $ship = explode("-", $ship[0]);
                        
                        $name = explode("||",$values->booking_customer_name);
                        if(count($name) > 1){
                            $name = str_replace('||','-',$values->booking_customer_name);
                        } else {
                            $name = $values->booking_customer_name;
                        }
                        
                        $payment_name               = $this->ketoan_model->getPaymentMethodById($values->payment_method)->payment_name;
                        //bank
                        if($values->payment_method == 3){
                            $getInfoTable               = $this->ketoan_model->getInfoTable(array('booking_id'=>$values->booking_id),'book_bankinfo_customers');
                            $_payment_name              = $this->ketoan_model->getListBanks($getInfoTable[0]->bankid);
                            $payment_name               = $payment_name."-".$_payment_name[0]->bank_name;
                        }
                        //Lọc ra những công nợ đã thu đầy đủ tiền.
//                        if($values->payment_method == 4){                          
//                            $paymentDebts           = $this->paymentDebts_model->find_by(array('booking_id' => $values->booking_id,'payment_collect'=>1,'payment_confirm'=>1));
//                            if(empty($paymentDebts)){
//                                continue;
//                            }
//                        }
                        
                        $loinhuan                               = $values->real_fare - $values->net_fare;                       
                        $lists[] = array(
                            'nowday'                            =>  $this->input->post('datepicker1'),
                            'name'                              =>  $name,
                            'booking_place_code'                =>  $values->booking_place_code,
                            'ship'                              =>  $ship[3].'-'.$ship[5],
                            'net_fare'                          =>  $this->bookinglib->my_number_format($values->net_fare,2, ',', ','),
                            'real_fare'                         =>  $this->bookinglib->my_number_format($values->real_fare,2, ',', ','),
                            'loinhuan'                          =>  $this->bookinglib->my_number_format($loinhuan,2, ',', ','),
                            'payment_name'                      =>  $payment_name,
                            'firstname'                         =>  $values->firstname,
                            'checker'                           =>  ($values->checker)?$this->ketoan_model->getInfoUserByParams($values->checker)->firstname:'----',
                            'sodu'                              =>  $values->sodu,
                            'payment_note'                      =>  $values->payment_note
                        );
                    }
                    
                    $this->view_data['lists']   =   $lists;
                    $this->load->view('auth/ketoan/export', $this->view_data);
               }
        }
}